$("document").ready(function(){
//初始化	
		var addrs_id=$(".address_start").parent("li").attr("data");
			$("#AddrsId").val(addrs_id);	
		var addressLength = parseInt($("#UsersAddress > li").length);
		//alert(addressLength);
		if(addressLength >3){
			$("#UsersAddress>li:gt(2)").hide();
			$(".address_select").show();
			
			$("#AddressSwitch").show();
			$("#AddressSwitch").removeClass("swith_on");
			$("#AddressSwitch >span").html("更多地址");
		}else{
			$("#AddressSwitch").hide();
		}
	
		
		if($(".address_select")){
			var addrs_id=$(".address_select").parent("li").attr("data")	;	
			$(".address_select").parent().find("input").prop('checked',true);	
			$("#AddrsId").val(addrs_id);
		}
		
		//$(".address_select").parent("li").show().siblings().hide();
	
	
//选择地址
	$(document).on('click', '.address_id', function() {
	
		$(this).addClass("address_select");
		$(this).parent("li").siblings().find(".address_id").removeClass("address_select");
		var addrs_id=$(".address_select").parent("li").attr("data");
		$("#AddrsId").val(addrs_id);
	});	

//更多地址
	$(document).on('click', '#AddressSwitch', function() {
		
		if($("#AddressSwitch").hasClass("swith_on")){
			$("#AddressSwitch").removeClass("swith_on");
			$("#AddressSwitch >span").html("更多地址");	
			
			if($("#UsersAddress >li> span").hasClass("address_select")){				
				$(".address_select").parent().show().siblings().hide();	
			}else{
				
				$("#UsersAddress > li:gt(2)").hide();
			}
		}else{
			$("#AddressSwitch").addClass("swith_on");
			$("#AddressSwitch > span").html("收起地址");
			$("#UsersAddress > li").show();
			
		}
	});	

//默认地址
	$(document).on('click', '.address_default', function() {
		$(this).parents("li").addClass('selected').siblings().removeClass('selected');
		$(this).parents("li").siblings().find(".address_id").removeClass("address_select");
		$(this).addClass("address_none");
		var address_current = $(".address_start");
		if(address_current){
			var address_currentValue=address_current.next().html();
			address_current.removeClass("address_select address_start");
			address_current.siblings().find(".address_default").removeClass("address_none")
			address_current.empty().html(address_currentValue+"<b></b>");
			var oldid= parseInt(address_current.parent("li").attr("data"));
		}
		$(this).parents("li").find(".address_id").addClass("address_select address_start").html("默认地址<b></b>");
		
		var newid= parseInt($(this).parents("li").attr("data"));
		if(newid !=0){
			$.ajax({url:"/Users/usersAddressSel/",data:{id:newid,cid:oldid},success:function(data){
				
				}});
		}
		var addrs_id=$("#UsersAddress .selected").attr("data");
		$(this).parent().parent().find("input").prop('checked',true);	
		$("#AddrsId").val(addrs_id);
	});	
	
//添加
	$(document).on('click', '#UsersAddressAdd', function() {	
		 layer.open({
			type: 2,
			shadeClose: true,
			title: '新增收货地址',
			closeBtn: 1,
			shade: [0.8, '#000'],
			border: [0],
			fix: true,
			offset: ['20px',''],
			area: ['800px', '350px'],
			content: '/Users/usersAddressAdd/',			
			end: function () {
			    //alert(123);
				$("#UsersAddress > li").find(".address_id").removeClass("address_select");	
				$("#UsersAddress > li:first").find(".address_id").addClass("address_select");
				
				var addrs_id=$(".address_select").parent().parent("li").attr("data");
				$("#AddrsId").val(addrs_id);
				//return;
			    //console.log(addrs_id);
				//alert($("#UsersAddress > li").length);
        		$("#AddressSwitch").toggleClass("swith_on");
        		if ($("#UsersAddress > li").length > 3) {
        		    //$("#AddressSwitch").show();
        		    $("#UsersAddress>li:gt(2)").hide();
        		    $(".address_select").show();

        		    $("#AddressSwitch").show();
        		    $("#AddressSwitch").removeClass("swith_on");
        		    $("#AddressSwitch >span").html("更多地址");
        		} else {
        		    $("#AddressSwitch").hide();
        		}
    		}
		}); 

	});	
	

//编辑
	$(document).on('click', '.address_editItem', function() {
	
			var addr_id = $(this).parents("li").attr("data");		
			if(addr_id){
			layer.open({
			type: 2,
			shadeClose: true,
			title: '编辑收货地址',
			closeBtn: [0, true],
			shade: [0.8, '#000'],
			border: [0],
			fix: false,
			offset: ['20px',''],
			area: ['800px', '350px'],
			content:'/Users/usersAddressAdd/id/'+addr_id,
			end: function(){
			
					$("#AddressSwitch").toggleClass("swith_on");
						if($("#UsersAddress > li").length >1){
						$("#AddressSwitch").show();
					}
			}
		}); 
			}
	});
	
//删除	
	$(document).on('click', '.address_del', function() {	
	
			var addr_li =$(this).parents("li");
			var addr_id = addr_li.attr("data");		
			
			if(addr_id){
				$.ajax({url:"/Users/usersAddressDel/id/"+addr_id,success:function(data){
					//$("#UsersAddressContainer").html(data);
					addr_li.remove();
					var addressLength = parseInt($("#UsersAddress > li").length);
					if(addressLength >3){
						$("#UsersAddress>li:gt(0)").hide();
						$("#AddressSwitch").show();
						$("#AddressSwitch").removeClass("swith_on");
						$("#AddressSwitch >span").html("更多地址");
					}else{
						$("#AddressSwitch").hide();
					}
				}});
			}
	});
//function scrollAddress(){	
//	$("#AddressPanel").slide({titCell:".hd ul",mainCell:".bd ul",autoPage:true,effect:"top",autoPlay:true,vis:3});
//}
});

		

